package com.car.slave.im.receiver;

import com.car.slave.im.IMClient;
import com.car.slave.im.model.user.IMUserMessage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 常驻通知，负责处理异地登陆，相当于推送.
 * @author zhangliucheng
 *
 */
public class IMNotificationReceiver extends BroadcastReceiver {
	
	private static final String otherLoginAction = "com.imchezhil.im.receiver.otherLogin";
	private static final String notifierAction = "com.imchezhil.im.receiver.notifier";
	
	public interface IMNotificationReceiverCallback {
		public void onOtherLogin(Context context);									// 异地登陆
		public void messageNotificater(Context context, IMUserMessage message);
	}

	public static IMNotificationReceiverCallback receiverCallback;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (otherLoginAction.equals(action)) {
			if (null != receiverCallback) {
				IMClient.shareInstance().logout(context);
				receiverCallback.onOtherLogin(context);
			}
		} else if (notifierAction.equals(action)) {
			if (null != receiverCallback) {
				IMUserMessage message = (IMUserMessage) intent.getSerializableExtra("data");
				receiverCallback.messageNotificater(context, message);
			}
		}
	}

	/**
	 * 发送异地登陆广播.
	 * @param context
	 */
	public static void postOtherLoginBroadcast(Context context) {
		Intent intent = new Intent(otherLoginAction);
		context.sendBroadcast(intent);
	}
	
	/**
	 * 发送通知广播.
	 * @param context
	 */
	public static void postNotificaterBroadcast(Context context, IMUserMessage message) {
		Intent intent = new Intent(notifierAction);
		intent.putExtra("data", message);
		context.sendBroadcast(intent);
	}
}
